package com.gxa.modules.service.drivingManagementService.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.RespResult;
import com.gxa.modules.dto.drivingManagement.CoachInfoDTO;
import com.gxa.modules.dto.drivingManagement.QueryCoachInfoDTO;
import com.gxa.modules.entity.CoachInfo;
import com.gxa.modules.mapper.drivingManagement.CoachInfoMapper;
import com.gxa.modules.service.drivingManagementService.CoachInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class CoachInfoServiceImpl implements CoachInfoService {

    @Autowired
    private CoachInfoMapper coachInfoMapper;


    @Override
    public RespResult addCoach(CoachInfo coachInfo) {//添加教练
        int result = coachInfoMapper.insert(coachInfo);

        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("添加失败！");
        }
    }

    @Override
    public RespResult queryCoachInfo(QueryCoachInfoDTO queryCoachInfoDTO) {//查询全部教练
        QueryWrapper<CoachInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.hasText(queryCoachInfoDTO.getCoachName()),"coachName",queryCoachInfoDTO.getCoachName())
                    .eq(StringUtils.hasText((CharSequence) queryCoachInfoDTO.getEntryTime()),"entryTime",String.valueOf(queryCoachInfoDTO.getEntryTime()))
                    .eq(StringUtils.hasText(String.valueOf(queryCoachInfoDTO.getMobilePhone())),"mobilePhone",String.valueOf(queryCoachInfoDTO.getMobilePhone()));

        Page<CoachInfo> carInfoPage = new Page<>(queryCoachInfoDTO.getPage(),queryCoachInfoDTO.getLimit());

        try{
            Page<CoachInfo> carInfoList = coachInfoMapper.selectPage(carInfoPage, queryWrapper);
            return RespResult.success(carInfoList);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败!");
        }
    }

    @Override
    public RespResult queryCoachByCoachNumberOrCoachName(Integer coachNumber, String coachName) {//根据教练编号和教练名称查询教练信息
        QueryWrapper<CoachInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(StringUtils.hasText(String.valueOf(coachNumber)),"coach_number", coachNumber).or()
                .eq(StringUtils.hasText(coachName),"coach_name", coachName);

        try{
            CoachInfo coachInfo = coachInfoMapper.selectOne(queryWrapper);
            return RespResult.success(coachInfo);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败!");
        }
    }

    @Override
    public RespResult updateCoach(CoachInfo coachInfo) {//更新教练信息

        int result = coachInfoMapper.updateById(coachInfo);

        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("更新失败！");
        }
    }
}
